package com.example.gameapplication;

import android.content.res.Resources;
import android.graphics.Paint;

import com.example.gameapplication.activity.MainActivity;
import com.example.gameapplication.database.DataUtils;
import com.example.gameapplication.plant.BigPlane;
import com.example.gameapplication.plant.BossPlane;
import com.example.gameapplication.plant.MiddlePlane;
import com.example.gameapplication.plant.MyPlane;
import com.example.gameapplication.plant.SmallPlane;
import com.example.gameapplication.plant.MainPlane;

import java.util.Random;

public abstract class BaseObject implements MyPlane {
    public  Random random;
    long score=0;
    public static int SEE = 1;
    public float midx;//对象的中心坐标x
    public float midy;//对象的中心坐标y
    public int speed;//移动速度
    public float leftx;//该对象的左上角x
    public float lefty;//该对象的左上角y
    public float rightx;//该对象的右下角x
    public float righty;//该对象的右下角y
    public int object_width;//该对象的宽度
    public int object_height;//该对象的高度
    public int currentFrame; // 战机等级，因为图片有三种战机，所以指定为3个等级
    public long life = 10;//生命数量，默认值
    public Resources resources;
    public Paint paint;
    public MainPlane mainPlane;
    public int lv = 1;
    private boolean isAlive;
    public long deathTime = 20;//播放死机体破坏的次数

    private long cd;//死亡标记，复活还没冷却也属于死亡

    public void reduceCD() {
        cd--;
    }

    public long getCd() {
        return cd;
    }

    public void reSetCD() {
        if (this instanceof BossPlane) {
            cd = DataUtils.getBossCd();
        } else if (this instanceof BigPlane) {
            cd = DataUtils.getBigCd();
        } else if (this instanceof MiddlePlane) {
            cd = DataUtils.getMiddleCd();
        } else if (this instanceof SmallPlane) {
            cd = DataUtils.getSmallCd();
        }
    }

    public void setCd(long cd) {
        this.cd = cd;
    }

    public long getScore() {
        return score;
    }

    public void setScore(long score) {
        this.score = score;
    }

    public int getLv() {
        return lv;
    }

    public void setLv(int lv) {
        this.lv = lv;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean alive) {
        if (!alive) {
            DataUtils.setAllScore(this);
            score = 0;//分数增加之后该战机就没有分数了,防止重复调用，重复计分数
            if (this instanceof MainPlane) {
                MainActivity.palySound(2,0);
            } else if (this instanceof SmallPlane) {
                MainActivity.palySound(3,0);
                DataUtils.addSmallKill();
            } else if (this instanceof MiddlePlane) {
                MainActivity.palySound(3,0);
                DataUtils.addMiddleKill();
            } else if (this instanceof BigPlane) {
                MainActivity.palySound(4,0);
                DataUtils.addBigKill();
            } else if (this instanceof BossPlane) {
                MainActivity.palySound(5,0);
                DataUtils.addBossKill();
            }
        } else {
            int speedChange=random.nextInt(5);
            if (this instanceof SmallPlane) {
                score = DataUtils.getSmallPlaneScore();
                speed=speedChange+DataUtils.getSmallEnemySpeed();
            } else if (this instanceof MiddlePlane) {
                score = DataUtils.getMiddlePlaneScore();
                speed=speedChange+DataUtils.getMiddleEnemySpeed();
            } else if (this instanceof BigPlane) {
                score = DataUtils.getBigPlaneScore();
                speed=speedChange+DataUtils.getBigEnemySpeed();
            } else if (this instanceof BossPlane) {
                score=DataUtils.getBosPlaneScore();
                speed =speedChange+ DataUtils.getBossEnemySpeed();
            }
            resetLife();//初始化生命值
            reSetCD();
        }
        isAlive = alive;
    }

    public BaseObject(Resources resources, Paint paint) {
        this.paint = paint;
        this.resources = resources;
        random = new Random(BaseObject.SEE);
        BaseObject.SEE += 1;
        initBitmap();
        setSeat(DataUtils.getGameViewWidth() / 2, 0);
        setAlive(true);//初始化生命和和分数
    }

    public long getLife() {
        return life;
    }

    public void setLife(long life) {
        this.life = life;
    }


    @Override
    public void setSpeed(int speed) {
        this.speed = speed;
    }

    @Override
    public float getMidx() {
        return midx;
    }

    @Override
    public void setMidx(float midx) {
        this.midx = midx;
    }


    public MainPlane getMainPlane() {
        return mainPlane;
    }

    public void setMainPlane(MainPlane mainPlane) {
        this.mainPlane = mainPlane;
    }

    @Override
    public float getMidy() {
        return midy;
    }

    @Override
    public void setMidy(float midy) {
        this.midy = midy;
    }

    //更新该对象的位置，传入中心点的坐标
    public void setSeat(float x, float y) {
        midx = x;
        midy = y;
        leftx = midx - object_width / 2;
        lefty = midy - object_height / 2;
        rightx = leftx + object_width;
        righty = lefty + object_height;
    }

    public void resetLife() {
        if (this instanceof SmallPlane) {
            life = lv * DataUtils.getSmallGrow();
        } else if (this instanceof MiddlePlane) {
            life = lv * DataUtils.getMiddleGraw();
        } else if (this instanceof BigPlane) {
            life = lv * DataUtils.getBigGraw();
        } else if (this instanceof BossPlane) {
            life = lv * DataUtils.getBossGraw();
        }
    }

    public int getMaxLifeValue() {
        if (this instanceof SmallPlane) {
            return lv * DataUtils.getSmallGrow();
        } else if (this instanceof MiddlePlane) {
            return lv * DataUtils.getMiddleGraw();
        } else if (this instanceof BigPlane) {
            return lv * DataUtils.getBigGraw();
        } else if (this instanceof BossPlane) {
            return lv * DataUtils.getBossGraw();
        }
        return 0;
    }

}
